$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/barticletypeinfo/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id',align: "center", width: 50, key: true},

			{ label: '分类名称', name: 'articleTypeName', index: 'articleTypeName',align: "center", width: 80 ,
				formatter:function(value,options,rowData){
					var id=rowData["id"];
					return "<a  onclick='update("+id+")'>"+value+"</a>";
				}
			},
			{ label: '状态', name: 'articleTypeStatus', index: 'articleTypeStatus',align: "center", width: 80 ,formatter:function(value,options,rowData) {
				if(value==1){
					return "开放";
				}else{
					return "关闭";
				}
				}
			},
			{ label: '操作', name: '', index: '',align: "center", width: 80 ,formatter:function(value,options,rowData) {
					var articleTypeStatus=rowData["articleTypeStatus"];
					var id=rowData["id"];
					if(articleTypeStatus==1) {
						return "<button class='btn btn-primary' onclick=vm.status('"+id+"','0')>" + '关闭' + "</button>";
					}else{
						return "<button class='btn btn-primary' onclick=vm.status('"+id+"','1')>" + '开放' + "</button>";
					}
				}
			}
        ],
		viewrecords: true,
		height: "100%",
		rowNum: 50,
		rowList : [12,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
setTimeout(function () {
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/barticletypeinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			 console.log(r);
			if(r.code==0){
				for(var i=0;i<r.barticletypeinfo.length;i++){
					$("#articleTypeName").append(
						"<option value=" + r.barticletypeinfo[i].articleTypeName +">"+"</option>");
				}
				$("#articleTypeNames").scrollTop()
			}
		}
	});
},2000)
	function update(e) {
		var id=e;
		vm.showList = false;
		vm.getInfo(id);
	}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bUserBrandInfo: {},
		bBrandInfo: {},
		brandinfo :{},
		barticletypeinfo:{},
		typeList:{},
		q:{
			brandNames:[]
		}
	},
	methods: {
		saveOrUpdate: function (event) {
			if(vm.barticletypeinfo.articleTypeName==""||vm.barticletypeinfo.articleTypeName==null) {
				alert("请输入分类名称");
				return;
			}

			var url = vm.barticletypeinfo.id == null ? "sys/barticletypeinfo/save" : "sys/barticletypeinfo/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.barticletypeinfo),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.back();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		status:function(e,x){
			vm.barticletypeinfo.id=e;
			vm.barticletypeinfo.articleTypeStatus=x;
			var msg="";
			if(x==0){
				msg="确认关闭此分类？"
			}else{
				msg="确认开放此分类？"
			}
			confirm(msg, function(){
				$.ajax({
					type: "POST",
					url: baseURL + "sys/barticletypeinfo/update",
					contentType: "application/json",
					data: JSON.stringify(vm.barticletypeinfo),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/barticletypeinfo/info/"+id, function(r){
				vm.barticletypeinfo=r.bArticleTypeInfo;
				$("#articleTypeLogo").attr("src", vm.barticletypeinfo.articleTypeLogo);
            });
		},
		back:function (event) {
			vm.showList = true;
			var page =$("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				page:page
			}).trigger("reloadGrid");
		}
	}
});